<template>
<span>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small">
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="下单开始日期：">
            <el-date-picker
              class="input-width"
              v-model="listQuery.startDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="请选择下单开始日期" @blur="validate_date()">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="下单结束日期：">
            <el-date-picker
              class="input-width"
              v-model="listQuery.endDate"
              value-format="yyyy-MM-dd"
              type="date"
              placeholder="请选择下单结束日期" @blur="validate_date()">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="订单状态：">
            <el-select v-model="listQuery.orderStatus" class="input-width" placeholder="全部" clearable>
              <el-option v-for="item in orderStatusOptions"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
                    <el-form-item label="支付状态：">
            <el-select v-model="listQuery.payStatus" class="input-width" placeholder="全部" clearable>
              <el-option v-for="item in payStatusOptions"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="平台：">
            <el-select v-model="listQuery.orderPlatform" class="input-width" placeholder="全部" clearable>
              <el-option v-for="item in orderPlatformOptions"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用户MID：">
            <el-input v-model="listQuery.userId" class="input-width" placeholder="请输入用户MID"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据统计</span>
    </el-card>
    <div class="total-layout">
      <el-row :gutter="20">
        <el-col :span="6">
          <div class="total-frame">
            <img :src="img_home_today_amount" class="total-icon">
            <div class="total-title">总金额</div>
            <div class="total-value">￥{{ amount.amountOrder }}</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="total-frame">
            <img :src="img_home_order" class="total-icon">
            <div class="total-title">订单总数</div>
            <div class="total-value">{{ amount.totalOrder }}</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="total-frame">
            <img :src="img_home_order" class="total-icon">
            <div class="total-title">发货成功</div>
            <div class="total-value">{{ amount.successOrder }}</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="total-frame">
            <img :src="img_home_order" class="total-icon">
            <div class="total-title">未完成</div>
            <div class="total-value">{{ amount.failOrder }}</div>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-table ref="orderTable"
                :data="list"
                style="width: 100%;"
                @selection-change="handleSelectionChange"
                v-loading="listLoading" border>
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="下单时间" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.createTime }}</template>
        </el-table-column>
        <el-table-column label="支付状态" align="center">
          <template slot-scope="scope">{{ scope.row.payStatus | formatPayStatus }}</template>
        </el-table-column>
        <el-table-column label="支付交易流水号" align="center">
          <template slot-scope="scope">{{ scope.row.transactionId }}</template>
        </el-table-column>
        <el-table-column label="平台" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.orderPlatform | formatOrderPlatform }}</template>
        </el-table-column>
        <el-table-column label="用户昵称" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.nickName }}</template>
        </el-table-column>
        <el-table-column label="用户MID" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.userId }}</template>
        </el-table-column>
        <el-table-column label="订单信息" align="center">
          <template slot-scope="scope">{{ scope.row.productName }}</template>
        </el-table-column>
        <el-table-column label="订单金额" width="120" align="center">
          <template slot-scope="scope">￥{{ scope.row.orderAmount }}</template>
        </el-table-column>
        <el-table-column label="订单状态" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.orderStatus | formatOrderStatus }}</template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleViewOrder(scope.$index, scope.row)">查看订单
            </el-button>
            <el-button
              size="mini"
              @click="handleCloseOrder(scope.$index, scope.row)" v-show="scope.row.status===0">关闭订单
            </el-button>
            <el-button
              size="mini"
              @click="handleDeliveryOrder(scope.$index, scope.row)" v-show="scope.row.status===1">订单发货
            </el-button>
            <el-button
              size="mini"
              @click="handleViewLogistics(scope.$index, scope.row)" v-show="scope.row.status===2||scope.row.status===3">订单跟踪
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDeleteOrder(scope.$index, scope.row)" v-show="scope.row.status===4">删除订单
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select
        size="small"
        v-model="operateType" placeholder="批量操作">
        <el-option
          v-for="item in operateOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <el-button
        style="margin-left: 20px"
        class="search-button"
        @click="handleBatchOperate()"
        type="primary"
        size="small">
        确定
      </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      title="关闭订单"
      :visible.sync="closeOrder.dialogVisible" width="30%">
      <span style="vertical-align: top">操作备注：</span>
      <el-input
        style="width: 80%"
        type="textarea"
        :rows="5"
        placeholder="请输入内容"
        v-model="closeOrder.content">
      </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeOrder.dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleCloseOrderConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <logistics-dialog v-model="logisticsDialogVisible"></logistics-dialog>
  </div>
</span>
</template>
<script>
import {fetchMobileList, closeOrder, deleteOrder} from '@/api/order'
import {formatDate} from '@/utils/date';
import LogisticsDialog from '@/views/oms/order/components/logisticsDialog';
import img_home_order from '@/assets/images/home_order.png';
import img_home_today_amount from '@/assets/images/home_today_amount.png';

const defaultListQuery = {
  pageNum: 1,
  pageSize: 20,
  startDate: null,
  endDate: null,
  orderPlatform: null,
  orderStatus: null,
  payStatus: null,
  userId: null
};
const defaultAmount = {
  amountOrder: 0.00,
  successOrder: 0,
  totalOrder: 0,
  failOrder: 0
};
export default {
  name: "orderList",
  components: {LogisticsDialog},
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      amount: Object.assign({}, defaultAmount),
      listLoading: true,
      list: null,
      total: null,
      operateType: null,
      multipleSelection: [],
      closeOrder: {
        dialogVisible: false,
        content: null,
        orderIds: []
      },
      orderStatusOptions: [
        {
          label: '全部',
          value: null
        },
        {
          label: '新订单',
          value: 0
        },
        {
          label: '已完结',
          value: 1
        },
        {
          label: '已取消',
          value: 2
        },
        {
          label: '已关闭',
          value: 3
        },
        {
          label: '已退款',
          value: 4
        }
      ],
      payStatusOptions: [
        {
          label: '全部',
          value: null
        },
        {
          label: '等待支付',
          value: 0
        },
        {
          label: '支付成功',
          value: 1
        },
        {
          label: '支付关闭',
          value: 2
        },
        {
          label: '支付失败',
          value: 3
        },
        {
          label: '退款成功',
          value: 4
        }
      ],
      orderPlatformOptions: [
        {
          label: '全部',
          value: null
        },
        {
          label: '百度',
          value: 'Baidu'
        },
        {
          label: '华为',
          value: 'Huawei'
        }
      ],
      operateOptions: [
        {
          label: "批量发货",
          value: 1
        },
        {
          label: "关闭订单",
          value: 2
        },
        {
          label: "删除订单",
          value: 3
        }
      ],
      logisticsDialogVisible: false,
      img_home_order,
      img_home_today_amount
    }
  },
  created() {
    this.getList()
  },
  filters: {
    formatCreateTime(time) {
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss');
    },
    formatOrderPlatform(value) {
      if (value === 'Baidu') {
        return '百度';
      } else if (value === 'Huawei') {
        return '华为';
      } else {
        return '其他';
      }
    },
    formatOrderStatus(value) {
      if (value === 0) {
        return '新订单';
      } else if (value === 1) {
        return '已完结';
      } else if (value === 2) {
        return '已取消';
      } else if (value === 3) {
        return '已关闭';
      }
    },
    formatPayStatus(value) {
      if (value === 0) {
        return '等待支付';
      } else if (value === 1) {
        return '支付成功';
      } else if (value === 2) {
        return '支付关闭';
      }
    },
  },
  methods: {
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      let now = formatDate(new Date(), 'yyyy-MM-dd')
      let end = formatDate(new Date(this.listQuery.endDate), 'yyyy-MM-dd')
      if (end <= now) {
        this.getList()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleViewOrder(index, row) {
      this.$router.push({path: '/oms/orderDetail', query: {id: row.id}})
    },
    handleCloseOrder(index, row) {
      this.closeOrder.dialogVisible = true;
      this.closeOrder.orderIds = [row.id];
    },
    handleDeliveryOrder(index, row) {
      let listItem = this.covertOrder(row);
      this.$router.push({path: '/oms/deliverOrderList', query: {list: [listItem]}})
    },
    handleViewLogistics(index, row) {
      this.logisticsDialogVisible = true;
    },
    handleDeleteOrder(index, row) {
      let ids = [];
      ids.push(row.id);
      this.deleteOrder(ids);
    },
    handleBatchOperate() {
      if (this.multipleSelection == null || this.multipleSelection.length < 1) {
        this.$message({
          message: '请选择要操作的订单',
          type: 'warning',
          duration: 1000
        });
        return;
      }
      if (this.operateType === 1) {
        //批量发货
        let list = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          if (this.multipleSelection[i].status === 1) {
            list.push(this.covertOrder(this.multipleSelection[i]));
          }
        }
        if (list.length === 0) {
          this.$message({
            message: '选中订单中没有可以发货的订单',
            type: 'warning',
            duration: 1000
          });
          return;
        }
        this.$router.push({path: '/oms/deliverOrderList', query: {list: list}})
      } else if (this.operateType === 2) {
        //关闭订单
        this.closeOrder.orderIds = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          this.closeOrder.orderIds.push(this.multipleSelection[i].id);
        }
        this.closeOrder.dialogVisible = true;
      } else if (this.operateType === 3) {
        //删除订单
        let ids = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          ids.push(this.multipleSelection[i].id);
        }
        this.deleteOrder(ids);
      }
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleCloseOrderConfirm() {
      if (this.closeOrder.content == null || this.closeOrder.content === '') {
        this.$message({
          message: '操作备注不能为空',
          type: 'warning',
          duration: 1000
        });
        return;
      }
      let params = new URLSearchParams();
      params.append('ids', this.closeOrder.orderIds);
      params.append('note', this.closeOrder.content);
      closeOrder(params).then(response => {
        this.closeOrder.orderIds = [];
        this.closeOrder.dialogVisible = false;
        this.getList();
        this.$message({
          message: '修改成功',
          type: 'success',
          duration: 1000
        });
      });
    },
    getList() {
      this.listLoading = true;
      fetchMobileList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.order.list;
        this.total = response.data.order.total;
        this.amount.amountOrder = response.data.amountOrder;
        this.amount.totalOrder = response.data.totalOrder;
        this.amount.successOrder = response.data.successOrder;
        this.amount.failOrder = response.data.failOrder
      });
    },
    deleteOrder(ids) {
      this.$confirm('是否要进行该删除操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = new URLSearchParams();
        params.append("ids", ids);
        deleteOrder(params).then(response => {
          this.$message({
            message: '删除成功！',
            type: 'success',
            duration: 1000
          });
          this.getList();
        });
      })
    },
    covertOrder(order) {
      let address = order.receiverProvince + order.receiverCity + order.receiverRegion + order.receiverDetailAddress;
      let listItem = {
        orderId: order.id,
        orderSn: order.orderSn,
        receiverName: order.receiverName,
        receiverPhone: order.receiverPhone,
        receiverPostCode: order.receiverPostCode,
        address: address,
        deliveryCompany: null,
        deliverySn: null
      };
      return listItem;
    },
    validate_date() {
      let startDate = this.listQuery.startDate
      let endDate = this.listQuery.endDate
      if (startDate != null && endDate != null) {
        if (startDate > endDate) {
          alert('结束日期不能大于开始日期')
          startDate = endDate
          return false
        }
      }
    }
  }
}
</script>
<style scoped>
.input-width {
  width: 203px;
}

.total-layout {
  margin-top: 20px;
}

.total-icon {
  color: #409EFF;
  width: 60px;
  height: 60px;
}

.total-title {
  position: relative;
  font-size: 16px;
  color: #909399;
  left: 70px;
  top: -50px;
}

.total-value {
  position: relative;
  font-size: 18px;
  color: #606266;
  left: 70px;
  top: -40px;
}
</style>


